package test02;

import java.util.LinkedList;
import java.util.List;

public class SingleLinkedList {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            this.val = x;
        }
    }

    public ListNode head;
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
        }
        else{
            ListNode temp = head;
            while (temp.next != null){
                temp = temp.next;
            }
            temp.next = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if(index < 0 || index > size()){
            return false;
        }
        if(index == 0){
            addFirst(data);
        }
        if(index == size()){
            addLast(data);
        }
        ListNode node = new ListNode(data);
        ListNode temp = head;
        while ((index-1) != 0){
            temp = temp.next;
            index--;
        }
        node.next = temp.next;
        temp.next = node;
        return true;

    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode temp = head;
        while (temp != null){
            if(temp.val == key){
                return true;
            }
            temp = temp.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode temp = head;
        if(head == null) {
            return;
        }
        if(contains(key)){
            while (temp.next != null){
                if(temp.next.val == key){
                temp.next = temp.next.next;
                }
                temp = temp.next;
            }
            if(temp.val == key){
                head = head.next;
                return;
            }
            return;
            } else {
            return;
        }

    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode temp = head;
        if(head == null) {
            return;
        }
        if(contains(key)){
            ListNode cur = head.next;
            while(cur != null){
                if(cur.val == key){
                    temp.next = cur.next;
                    cur = cur.next;
                }else {
                    temp = cur;
                    cur = cur.next;
                }

            }
            if(head.val == key){
                head = head.next;
            }
        }

    }
    //得到单链表的长度
    public int size(){
        int len = 0;
        ListNode cur = head;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }
    public void display(){
        ListNode temp = head;
        while(temp != null){
            System.out.print(temp.val + " ");
        }
    }
    public void clear(){
        head = null;
    }
}